import qs from 'qs';
import {
    myHome
} from '../api';


export default {
    namespace: 'myHome',
    state: {
        data: [],
        status: '',
        companyStatus: '1',
        Inst: {},
        province: [],
        urlStatusData: '',
        company: []
    },
    reducers: {
        queryListSuccess(state, {
            payload
        }) {
            return {
                ...state,
                ...payload
            };
        }
    },
    effects: {
        * queryStatistics({
            payload: params
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(myHome.queryStatistics, params);
            yield put({
                type: 'queryListSuccess',
                payload: {
                    Statistics: jsonResult.data
                }
            });
        },
        * serch({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(myHome.query, params);
            console.log(3333)
            console.log(jsonResult)
            yield put({
                type: 'serchSuccess',
                payload: {
                    data: jsonResult.data
                }
            });
            if (typeof func === 'function') {
                func(jsonResult);
            }
        },
        * queryProvince({
            payload: params
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(myHome.queryarea, params);
            yield put({
                type: 'queryListSuccess',
                payload: {
                    province: jsonResult.data
                }
            });

        },
        * queryRegion({
            payload: params
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(myHome.queryarea, params);
            yield put({
                type: 'queryListSuccess',
                payload: {
                    region: jsonResult.data
                }
            });

        },
        * queryCity({
            payload: params
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(myHome.queryarea, params);
            yield put({
                type: 'queryListSuccess',
                payload: {
                    city: jsonResult.data
                }
            });

        },
        * update({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(myHome.update, params);
            if (typeof func === 'function') {
                func();
            }
        },
        * setStatus({
            payload: params
        }, {
            call,
            put
        }) {
            yield put({
                type: 'queryListSuccess',
                payload: {
                    status: params
                }
            });
        },
        * companyStatus({
            payload: params
        }, {
            call,
            put
        }) {
            yield put({
                type: 'queryListSuccess',
                payload: {
                    companyStatus: params
                }
            });
        },
        * urlStatus({
            payload: params
        }, {
            call,
            put
        }) {
            console.log(params)
            yield put({
                type: 'queryListSuccess',
                payload: {
                    urlStatusData: params
                }
            });
        },
        * getCompany({
            payload: params
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(myHome.getCompany, params);
            yield put({
                type: 'queryListSuccess',
                payload: {
                    company: jsonResult.data
                }
            });

        },
        * Logout({
            payload: params
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(myHome.Logout, params)
        },
        * querySummaryByDate({
            payload: params
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(myHome.querySummaryByDate, params);
            yield put({
                type: 'queryListSuccess',
                payload: {
                    SummaryByDate: jsonResult.data
                }
            });
        },
        * querySummaryByMonth({
            payload: params
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(myHome.querySummaryByMonth, params);
            yield put({
                type: 'queryListSuccess',
                payload: {
                    SummaryByMonth: jsonResult.data
                }
            });
        },
        * querySummary({
            payload: params
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(myHome.querySummary, params);
            yield put({
                type: 'queryListSuccess',
                payload: {
                    Summary: jsonResult.data
                }
            });
        },
        * getAmapinfo({
            payload: params
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(myHome.getAmapinfo, params);
            yield put({
                type: 'queryListSuccess',
                payload: {
                    amapData: jsonResult.geocodes
                }
            });
        },
        * serchfun({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(myHome.personalQuery, params);
            if (typeof func === 'function') {
                func(jsonResult.data);
            }
        },
        * reload(action, {
            put,
            select
        }) {
            const data = yield select(state => state.data);
            yield put({
                type: 'fetch',
                payload: {
                    data
                }
            });
        }
    }
};